package vn.conghuy.talkwithme.responsor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import vn.conghuy.talkwithme.responsor.responsor.CalendarResponsor;
import vn.conghuy.talkwithme.responsor.responsor.CallResponsor;
import vn.conghuy.talkwithme.responsor.responsor.EmailResponsor;
import vn.conghuy.talkwithme.responsor.responsor.FacebookResponsor;
import vn.conghuy.talkwithme.responsor.responsor.FootballResponsor;
import vn.conghuy.talkwithme.responsor.responsor.MapResponsor;
import vn.conghuy.talkwithme.responsor.responsor.OpenAppResponsor;
import vn.conghuy.talkwithme.responsor.responsor.ParamTemplateResponsor;
import vn.conghuy.talkwithme.responsor.responsor.Responsor;
import vn.conghuy.talkwithme.responsor.responsor.SearchResponsor;
import vn.conghuy.talkwithme.responsor.responsor.SmsResponsor;
import vn.conghuy.talkwithme.responsor.responsor.TemplateResponsor;
import vn.conghuy.talkwithme.responsor.responsor.WolframResponsor;
import android.content.Context;

public class NormalResponsorManager extends ResponsorManager {
	
	/**
	 * @author  Huy
	 */
	class PriorResponsorVerbIndex{
		
		public Responsor responsor;
		public int verbIndex;
		
		public PriorResponsorVerbIndex(Responsor responsor, int verbIndex){
			this.responsor = responsor;
			this.verbIndex = verbIndex;
		}
	}
	
	class VerbIndexResponsorComparator implements Comparator<PriorResponsorVerbIndex>{

		@Override
		public int compare(PriorResponsorVerbIndex arg0,
				PriorResponsorVerbIndex arg1) {
			return arg0.verbIndex - arg1.verbIndex;
		}
	}
	
	String wolframApiKey;
	Context context;
	ArrayList<Responsor> priorResponsors;
	ArrayList<PriorResponsorVerbIndex> priorResponsorsCanResponse;
	
	SmsResponsor smsResponsor;

	public NormalResponsorManager(){
		super();
		priorResponsors = new ArrayList<Responsor>();
		priorResponsorsCanResponse = new ArrayList<PriorResponsorVerbIndex>();
	}
	
	public void setWolframApiKey(String wolframApiKey) {
		this.wolframApiKey = wolframApiKey;
	}
	
	public void setContext(Context context) {
		this.context = context;
	}

	public void create(){
		priorResponsors.add(new CalendarResponsor(context));
		smsResponsor = new SmsResponsor(context);
		priorResponsors.add(smsResponsor);
		priorResponsors.add(new CallResponsor(context));
		priorResponsors.add(new EmailResponsor(context));
		priorResponsors.add(new OpenAppResponsor(context));
		
		responsors = new ArrayList<Responsor>();
		
		responsors.add(new TemplateResponsor());
		responsors.add(new ParamTemplateResponsor());
		responsors.add(new FacebookResponsor(context));
		responsors.add(new FootballResponsor());
		responsors.add(new SearchResponsor(context, true));
		responsors.add(new MapResponsor(context));
		responsors.add(new WolframResponsor(wolframApiKey, context));
		responsors.add(new SearchResponsor(context, false));
	}
	
	@Override
	public String Response(String request) {

		//priority curResponsor
		if(curResponsor != null){
			int relative = curResponsor.relativeWithLastRequest(request);
			if(relative != Responsor.RELATIVE_NONE){
				String result = curResponsor.response(request);
				if(result != null) return result;
			}
			else curResponsor.reset();
		}
		
		String result;
		
		findPriorResponsor(request);
		for(PriorResponsorVerbIndex responsorVerbIndex: priorResponsorsCanResponse){
			curResponsor = responsorVerbIndex.responsor;
			result = curResponsor.response(request);
			if(result != null){
				return result;
			}
		}
		
		//second, use normal responsor
		for(Responsor responsor: responsors){
			curResponsor = responsor;
			result = curResponsor.response(request);
			if(result != null){
				return result;
			}
		}
		return null;
	}

	private void findPriorResponsor(String request) {
		for(Responsor responsor:priorResponsors){
			int indexOfMainVerb = responsor.indexOfMainVerb(request);
			if(indexOfMainVerb >= 0){
				priorResponsorsCanResponse.add(new PriorResponsorVerbIndex(responsor, indexOfMainVerb));
			}
		}
		Collections.sort(priorResponsorsCanResponse, new VerbIndexResponsorComparator());
	}
	
	public void reset(){
		//smsResponsor.resetAfterResume();
	}
}
